{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 02, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Matrices: Two Dimensional Lists </h2>\n",
    "\n",
    "A matrix is a list of vectors with the same dimensions.\n",
    "\n",
    "Here is an example matrix formed by 4 row vectors with dimension 5:\n",
    "\n",
    "$$\n",
    "    M = \\mymatrix{rrrrr}{8 & 0 & -1 & 0 & 2 \\\\ -2 & -3 & 1 & 1 & 4 \\\\ 0 & 0 & 1 & -7 & 1 \\\\ 1 & 4 & -2 & 5 & 9}.\n",
    "$$\n",
    "\n",
    "We can also say that $M$ is formed by 5 column vectors with dimension 4.\n",
    "\n",
    "$M$ is called an $ (4 \\times 5) $-dimensional matrix. ($4 \\times 5$: \"four times five\") \n",
    "\n",
    "We can represent $M$ as a two dimensional list in python.    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can break lines when defining our list\n",
    "M = [ \n",
    "    [8 , 0 , -1 , 0 , 2], \n",
    "    [-2 , -3 , 1 , 1 , 4], \n",
    "    [0 , 0 , 1 , -7 , 1],\n",
    "    [1 , 4 , -2 , 5 , 9]\n",
    "] \n",
    "\n",
    "# let's print matrix M\n",
    "print(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's print M in matrix form, row by row\n",
    "\n",
    "for i in range(4): # there are 4 rows\n",
    "    print(M[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remark that, by definition, the rows and columns of matrices are indexed starting from 1.\n",
    "\n",
    "The $ (i,j) $-th entry of $ M $ refers to the entry in $ i $-th row and $ j $-th column.\n",
    "\n",
    "(It is also denoted as $ M[i,j] $, $ M(i,j) $, or $ M_{ij} $.)\n",
    "\n",
    "On the other hand, in python, the indices start from zero.\n",
    "\n",
    "So, when we define a list for a matrix or vector in python, the value of an index is one less than the value of original index.\n",
    "\n",
    "Here are a few examples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "M = [ \n",
    "    [8 , 0 , -1 , 0 , 2], \n",
    "    [-2 , -3 , 1 , 1 , 4], \n",
    "    [0 , 0 , 1 , -7 , 1],\n",
    "    [1 , 4 , -2 , 5 , 9]\n",
    "] \n",
    "\n",
    "#let's print the element of M in the 1st row and the 1st column.\n",
    "print(M[0][0])\n",
    "\n",
    "#let's print the element of M in the 3rd row and the 4th column.\n",
    "print(M[2][3])\n",
    "\n",
    "#let's print the element of M in the 4th row and the 5th column.\n",
    "print(M[3][4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Multiplying a matrix with a number </h3>\n",
    "\n",
    "Let's multiply matrix $ M $ by $ -2 $. \n",
    "\n",
    "Each entry is multiplied by $ -2 $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we use double nested for-loops\n",
    "\n",
    "N =[] # the result matrix\n",
    "\n",
    "for i in range(4): # for each row\n",
    "    N.append([]) # create an empty sub-list for each row in the result matrix\n",
    "    for j in range(5): # in row (i+1), for each column\n",
    "        N[i].append(M[i][j]*-2) # we add new elements into the i-th sub-list\n",
    "        \n",
    "# let's print M and N, and see the results\n",
    "print(\"I am M:\")\n",
    "for i in range(4):\n",
    "    print(M[i])\n",
    "\n",
    "print()\n",
    "\n",
    "print(\"I am N:\")\n",
    "for i in range(4):\n",
    "    print(N[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After executing our program, we can write down the matrix $ N= -2 \\cdot M $:\n",
    "\n",
    "$$\n",
    "  N= -2 M = \\mymatrix{rrrrr}{-16 & 0 & 2 & 0 & -4 \\\\ 4 & 6 & -2 & -2 & -8 \\\\ 0 & 0 & -2 & 14 & -2 \\\\ -2 & -8 & 4 & -10 & -18}.\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> The summation of matrices</h3>\n",
    "\n",
    "If $ M $ and $ N $ are matrices with the same dimensions, then $ M+N $ is also a matrix with the same dimension.\n",
    "\n",
    "The summation of two matrices is similar to the summation of two vectors. \n",
    "\n",
    "If $ K = M +N $, then $ K[i,j] = M[i,j] + N[i,j] $ for every pair of $ (i,j) $.\n",
    "\n",
    "Let's find $ K $ in python. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create an empty list for the result matrix\n",
    "K=[]\n",
    "\n",
    "for i in range(len(M)): # len(M) return the number of rows in M\n",
    "    K.append([]) # we create a new row for K\n",
    "    for j in range(len(M[0])): # len(M[0]) returns the number of columns in M\n",
    "        K[i].append(M[i][j]+N[i][j]) # we add new elements into the i-th sublist/rows\n",
    "\n",
    "# print each matrix in a single line\n",
    "print(\"M=\",M)\n",
    "print(\"N=\",N)\n",
    "print(\"K=\",K)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> Observation:</b>\n",
    "\n",
    "$ K = N +M  $. We defined $ N $ as $ -2 M $. \n",
    "\n",
    "Thus, $ K = N+M = -2M + M = -M $.\n",
    "\n",
    "We can see that $ K = -M $ by looking the outcomes of our program."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1 </h3>\n",
    "\n",
    "Randomly create $ (3 \\times 4) $-dimensional matrices $ A $ and $ B $. \n",
    "\n",
    "The entries can be from the list $ \\{-5,\\ldots,5\\} $.\n",
    "\n",
    "Print the entries of both matrices.\n",
    "\n",
    "Find matrix $ C = 3A - 2B $, and print its entries. (<i>Note that $ 3A - 2B = 3A + (-2B) $</i>.)\n",
    "\n",
    "Verify the correctness your outcomes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B20_Python_Lists_Matrices_Solutions.ipynb#task1\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Transpose of a matrix</h3>\n",
    "\n",
    "The transpose of a matrix is obtained by interchanging rows and columns. \n",
    "\n",
    "For example, the second row becomes the new second column, and third column becomes the new third row.\n",
    "\n",
    "The transpose of a matrix $ M $ is denoted $ M^T $.\n",
    "\n",
    "Here we give two examples.\n",
    "\n",
    "$$\n",
    "    M = \\mymatrix{rrrr}{-2 & 3 & 0 & 4\\\\ -1 & 1 & 5 & 9} ~~~~~ \\Rightarrow ~~~~~ M^T=\\mymatrix{rr}{-2 & -1 \\\\ 3 & 1 \\\\ 0 & 5 \\\\ 4 & 9}  ~~~~~~~~ \\mbox{ and } ~~~~~~~~\n",
    "    N = \\mymatrix{ccc}{1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9} ~~~~~ \\Rightarrow ~~~~~ N^T = \\mymatrix{ccc}{1 & 4 & 7 \\\\ 2 & 5 & 8 \\\\ 3 & 6 & 9}.\n",
    "$$\n",
    "\n",
    "Shortly, $ M[i,j] = M^T[j,i] $ and $ N[i,j] = N^T[j,i] $. (The indices are interchanged.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2 </h3>\n",
    "\n",
    "Calculate $ M^T $ and $ N^T $ in python.\n",
    "\n",
    "Print all matrices and verify the correctness of your outcome."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "M = [\n",
    "    [-2,3,0,4],\n",
    "    [-1,1,5,9]      \n",
    "]\n",
    "N =[\n",
    "    [1,2,3],\n",
    "    [4,5,6],\n",
    "    [7,8,9]\n",
    "]\n",
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B20_Python_Lists_Matrices_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Multiplication of a matrix with a vector </h3>\n",
    "\n",
    "We define a matrix $ M $ and a column vector $ v $:\n",
    "\n",
    "$$\n",
    "    M = \\mymatrix{rrr}{-1 & 0 & 1 \\\\ -2 & -3 & 4 \\\\ 1 & 5 & 6} ~~~~~~\\mbox{and}~~~~~~ v = \\myrvector{1 \\\\ -3 \\\\ 2}.\n",
    "$$\n",
    "\n",
    "The multiplication of $ M v $ is a new vector $ u $ shown as $ u = M v $:\n",
    "<ul>\n",
    "    <li> The first entry of $u $ is the inner product of the first row of $ M $ and $ v $.</li>\n",
    "    <li> The second entry of $ u $ is the inner product of the second row of $M$ and $ v $.</li>\n",
    "    <li> The third entry of $ u $ is the inner product of the third row of $M$ and $v$. </li>\n",
    "</ul>\n",
    "\n",
    "Let's do the calculations in python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# matrix M\n",
    "M = [\n",
    "    [-1,0,1],\n",
    "    [-2,-3,4],\n",
    "    [1,5,6]\n",
    "]\n",
    "\n",
    "# vector v\n",
    "v = [1,-3,2]\n",
    "\n",
    "# the result vector u\n",
    "u = []\n",
    "\n",
    "# for each row, we do an inner product\n",
    "for i in range(3):\n",
    "    # inner product for one row is initiated\n",
    "    inner_result = 0 # this variable keeps the summation of the pairwise multiplications\n",
    "    for j in range(3): # the elements in the i-th row\n",
    "        inner_result = inner_result + M[i][j] * v[j]\n",
    "    # inner product for one row is completed\n",
    "    u.append(inner_result)\n",
    "\n",
    "print(\"M is\")\n",
    "for i in range(len(M)):\n",
    "    print(M[i])\n",
    "print()\n",
    "print(\"v=\",v)\n",
    "print()\n",
    "print(\"u=\",u)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's check the calculations:\n",
    "\n",
    "$$\n",
    " \\mbox{First row:}~~~~ \\biginner{ \\myrvector{-1 \\\\ 0 \\\\ 1} }{ \\myrvector{1 \\\\ -3 \\\\ 2} } = (-1)\\cdot 1 + 0 \\cdot (-3) + 1 \\cdot 2 = -1 + 0 + 2 = -1.\n",
    "$$\n",
    "$$\n",
    " \\mbox{Second row:}~~~~ \\biginner{ \\myrvector{-2 \\\\ -3 \\\\ 4} }{ \\myrvector{1 \\\\ -3 \\\\ 2} } = (-2)\\cdot 1 + (-3) \\cdot (-3) + 4 \\cdot 2 = -2 + 9 + 8 = 15.\n",
    "$$\n",
    "$$\n",
    " \\mbox{Third row:}~~~~ \\biginner{ \\myrvector{1 \\\\ 5 \\\\ 6} }{ \\myrvector{1 \\\\ -3 \\\\ 2} } = 1\\cdot 1 + 5 \\cdot (-3) + 6 \\cdot 2 = 1 - 15 + 12 = -2.\n",
    "$$\n",
    "\n",
    "Then,\n",
    "$$\n",
    "    v' = \\myrvector{-1 \\\\ 15 \\\\ -2 }.\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>Observations:</b> \n",
    "<ul>\n",
    "    <li> The dimension of the row of $ M $ is the same as the dimension of $ v $. Otherwise, the inner product is not defined.</li>\n",
    "    <li> The dimension of the result vector is the number of rows in $ M $, because we have an inner product for each row of $ M $</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 3 </h3>\n",
    "\n",
    "Find $ u' = N u $ in python for the following matrix $ N $ and column vector $ u $:\n",
    "\n",
    "$$\n",
    "    N = \\mymatrix{rrr}{-1 & 1 & 2 \\\\ 0 & -2 & -3 \\\\ 3 & 2 & 5 \\\\ 0 & 2 & -2} ~~~~~~\\mbox{and}~~~~~~ u = \\myrvector{2 \\\\ -1 \\\\ 3}.\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B20_Python_Lists_Matrices_Solutions.ipynb#task3\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Multiplication of two matrices </h3>\n",
    "\n",
    "This is just the generalization of the procedure given above.\n",
    "\n",
    "Let's find matrix $ K = M \\cdot N $ for given matrices\n",
    "$\n",
    "    M = \\mymatrix{rrr}{-1 & 0 & 1 \\\\ -2 & -1 & 2 \\\\ 1 & 2 & -2} ~~\\mbox{and}~~ \n",
    "    N = \\mymatrix{rrr}{0 & 2 & 1 \\\\ 3 & -1 & -2 \\\\ -1 & 1 & 0}.\n",
    "$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remark that the matrix $ N $ has three columns: $ v_1 = \\myrvector{0 \\\\ 3 \\\\ -1} $, $ v_2 = \\myrvector{2 \\\\ -1 \\\\ 1} $, and $ v_3 = \\myrvector{1 \\\\ -2 \\\\ 0} $.\n",
    "\n",
    "We know how to calculate $ v_1' = M \\cdot v_1 $. \n",
    "\n",
    "Similarly, we can calculate $ v_2' = M \\cdot v_2 $ and $ v_3' = M \\cdot v_3 $. \n",
    "\n",
    "You may have already guessed that these new column vectors ($v_1'$, $v_2'$, and $v_3'$) are the columns of $ K $. \n",
    "\n",
    "Technically, the inner product of the i-th row of $ M $ and $ j $-th row of $ N $ gives the $(i,j)$-th entry of $ K $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 4 </h3>\n",
    "\n",
    "Find matrix $ K $.\n",
    " \n",
    "This is a challanging task. You may use triple nested for-loops. \n",
    "\n",
    "You may also consider to write a function taking two lists and returning their inner product."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# matrix M\n",
    "M = [\n",
    "    [-1,0,1],\n",
    "    [-2,-1,2],\n",
    "    [1,2,-2]\n",
    "]\n",
    "\n",
    "# matrix N\n",
    "M = [\n",
    "    [0,2,1],\n",
    "    [3,-1,-2],\n",
    "    [-1,1,0]\n",
    "]\n",
    "\n",
    "# matrix K\n",
    "K = []\n",
    "\n",
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B20_Python_Lists_Matrices_Solutions.ipynb#task4\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Is $ A  B = B  A $? </h3>\n",
    "\n",
    "It is a well-known fact that the order of numbers does not matter in multiplication.\n",
    "\n",
    "For example, $ (-3) \\cdot 4 = 4 \\cdot (-3) $.\n",
    "\n",
    "Is it also true for matrices? For any given two matrices $ A $ and $ B $, is $ A \\cdot B = B \\cdot A $?\n",
    "\n",
    "There are some examples of $A$ and $B$ such that $ A \\cdot B = B \\cdot A $.\n",
    "\n",
    "But this is not true for most of the cases, and so this statement is false. \n",
    "\n",
    "We can falsify this statement by using our programming skill.\n",
    "\n",
    "We can also follow a probabilistic strategy. \n",
    "\n",
    "The idea is as follows: Randomly find two example matrices $ A $ and $ B $ such that $ AB \\neq BA $.\n",
    "\n",
    "Remark that if  $ AB = BA $, then $ AB - BA $ is a zero matrix."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 5 </h3>\n",
    "\n",
    "Randomly define two $ (2 \\times 2) $-dimensional matrices $A$ and $ B $. \n",
    "\n",
    "Then, find $ C= AB-BA $. If $ C $ is not a zero matrix, then we are done.\n",
    "\n",
    "<i>Remark: With some chances, we may find a pair of $ (A,B) $ such that $ AB = BA $. \n",
    "    \n",
    "In this case, we should repeat our experiment. </i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B20_Python_Lists_Matrices_Solutions.ipynb#task5\">click for our solution</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
